package chapter10.eg12;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.StaxDriver;

public class BeanXmlConvertApp {

  public static void main(String[] args) {
    XStream xstream = new XStream(new StaxDriver());

    //XStream的XML输出更简洁,可以为您的自定义类名创建别名XML元素名称。这是唯一类型的映射需要使用XStream甚至是可选的。
    xstream.alias("person", Person.class);
    xstream.alias("phonenumber", PhoneNumber.class);

    Person joe = new Person("Joe", "Walnes");
    joe.setPhone(new PhoneNumber(123, "1234-456"));
    joe.setFax(new PhoneNumber(123, "9999-999"));

    //bean to XML
    String xml = xstream.toXML(joe);
    //XML to bean
    Person newJoe = (Person)xstream.fromXML(xml);

    System.out.println(newJoe.getFirstname());

    System.out.println(xml);
  }

}
